package db.dbo;

import java.util.ArrayList;
import java.util.List;

import db.dao.DictionariesDAO;
import db.dao.LanguagesDAO;
import db.dao.ManagerInDictDAO;
import db.dao.UserDAO;
import db.dao.WordsDAO;
import db.dto.DictApproverDTO;
import db.dto.DictReviewerDTO;
import db.dto.DictionariesDTO;
import db.dto.LanguagesDTO;
import db.dto.ManagerInDictDTO;
import db.dto.UserDTO;

public class ListDictionariesDBO {
	public List<DictReviewerDTO> getListDictionariesReviewer(String username) {
		
		ArrayList<DictReviewerDTO> listDict = new ArrayList<DictReviewerDTO>();
		
		DictReviewerDTO dictReviewerDTO = null;
		
		UserDAO userDAO = new UserDAO();
		
		
		UserDTO userDTO = userDAO.getUser(username);
		
		ManagerInDictDAO managerInDictDAO = new ManagerInDictDAO();
		
		List<ManagerInDictDTO> listManagerInDictDTO = managerInDictDAO.getManagerInDictUser(userDTO.getId());
		
		for(int i = 0; i < listManagerInDictDTO.size(); i++) {
			
			DictionariesDAO dictDAO = new DictionariesDAO();
			
			DictionariesDTO dictDTO = dictDAO.getDictionary(listManagerInDictDTO.get(i).getDictID());
			
			LanguagesDAO lanDAO = new LanguagesDAO();
			
			LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
			
			LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
			
			dictReviewerDTO = new DictReviewerDTO();
			
			dictReviewerDTO.setDictName(from.getName()+" - "+to.getName());
			
			WordsDAO wordDAO = new WordsDAO();
			
			dictReviewerDTO.setNewWordCount(wordDAO.getWordReviewerCount(dictDTO.getId()));
			
			dictReviewerDTO.setDictID(dictDTO.getId());
			
			listDict.add(dictReviewerDTO);
		}
		
		return listDict;
	}
	
//	public static void main(String args[]) {
//		ListDictionariesDBO listDict = new ListDictionariesDBO();
//		List<DictApproverDTO> list = listDict.getListDictionariesApprover("HackerVN");
//	}
	
	public List<DictApproverDTO> getListDictionariesApprover(String username) {
		
		ArrayList<DictApproverDTO> listDict = new ArrayList<DictApproverDTO>();
		
		DictApproverDTO dictApproverDTO = null;
		
		UserDAO userDAO = new UserDAO();
		
		ManagerInDictDAO managerInDictDAO = new ManagerInDictDAO();
		
		DictionariesDAO dictDAO = new DictionariesDAO();
		
		LanguagesDAO lanDAO = new LanguagesDAO();
		
		WordsDAO wordDAO = new WordsDAO();
		
		UserDTO userDTO = userDAO.getUser(username);
		
		List<ManagerInDictDTO> managerInDictDTO = managerInDictDAO.getManagerInDictUser(userDTO.getId());
		
		for(int i = 0; i < managerInDictDTO.size(); i++) {
			dictApproverDTO = new DictApproverDTO();
			
			DictionariesDTO dictDTO = dictDAO.getDictionary(managerInDictDTO.get(i).getDictID());
			
			LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
			
			LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
			
			dictApproverDTO.setDictName(from.getName()+" - "+to.getName());
			
			dictApproverDTO.setDictID(dictDTO.getId());
			
			dictApproverDTO.setNewWordCount(wordDAO.getWordApproverCount(dictDTO.getId()));
			
			List<ManagerInDictDTO> managerDTO = managerInDictDAO.getManagerInDictDict(dictDTO.getId());
			
			List<UserDTO> reviewer = new ArrayList<UserDTO>();
			
			for(int j = 0; j < managerDTO.size(); j++) {
				
				UserDTO review = userDAO.getUser(managerDTO.get(j).getUserID());
				
				if(review.getRole() == 1) {
					reviewer.add(review);
				}
			}
			dictApproverDTO.setListReviewer(reviewer);
			listDict.add(dictApproverDTO);
		}
		
		return listDict;
	}
}
